package com.hzsd.zengbanyi.biaodan_text;

import android.app.Activity;
import android.app.Application;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Environment;

import java.util.LinkedList;
import java.util.List;

import hzst.android.util.CrashHandler;
import hzst.android.util.CrashHandler.OnCrashListener;

public class CrashApplication extends Application {
	private static CrashApplication instance;
	private static List<Activity> mList = new LinkedList<Activity>();
	 public void onCreate() {  
	        super.onCreate();  

	        String crashPath = null;
	        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {      
	        	crashPath = "/mnt/sdcard/crash/";      
	        }
	        CrashHandler crashHandler = CrashHandler.getInstance();
	        crashHandler.init(this, crashPath, new OnCrashListener() {
				
				@Override
				public void sendLogToServer(String exString) {
					Intent intent = new Intent();
					intent.setAction("hzst.startech.hjjc.service.SendErrorLogService");
					//怎么将错误信息传给service
					SharedPreferences sp=getSharedPreferences("errorlog", MODE_PRIVATE);
					Editor editor=sp.edit();
					editor.putString("exString", exString);
					editor.commit();
					startService(intent);
				}
				
				@Override
				public void exitApp() {
					//退出程序      
					exit(); 
		            
				}
			});
	 }
	    public void addActivity(Activity activity) {
	        mList.add(activity); 
	    }
	    public static void exit() {
	        try { 
	            for (Activity activity : mList) {
	                if (activity != null) 
	                    activity.finish(); 
	            } 
	        } catch (Exception e) {
	            e.printStackTrace();
	        } finally { 
	            System.exit(0); 
	        } 
	    }
	    public synchronized static CrashApplication getInstance() {
	        if (null == instance) {
	            instance = new CrashApplication(); 
	        } 
	        return instance; 
	    }
	 
}
